package mao.exaimnation.netease1;

import java.util.*;

/**
 * @author Totoro
 * @date 2020/9/12
 * @explain:
 */
public class Main1 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        while (scanner.hasNext()){
            int n = scanner.nextInt();
            int m = scanner.nextInt();
            ArrayList<Integer>[] relation = new ArrayList[n];
            Set<Integer> set = new HashSet<>();
            int res = 0;
            for (int i = 0; i < m; i++) {
                int parent = scanner.nextInt();
                String next = scanner.next();
                int son = scanner.nextInt();
                if (relation[parent] == null){
                    relation[parent] = new ArrayList<>(2);
                }
                relation[parent].add(son);
                set.add(parent);
            }

            for (int i = 0; i < relation.length; i++) {
                ArrayList<Integer> cur = relation[i];
                if (cur == null) continue;
                if (cur.size() != 2) continue;
                if (set.contains(cur.get(0))) continue;
                if (set.contains(cur.get(1))) continue;
                res++;
            }
            System.out.println(res);
        }
    }
}
